package com.atguigu.yygh.common.result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;
// 统一结果返回类
@Data
public class R {
    private Integer code;
    private Boolean success;
    private String message;
    private Map<String,Object> data = new HashMap<String,Object>();
    private R(){}// 构造方法私有化 外界无法创建R对象
    public static R ok (){
        R r = new R();
        r.code = REnum.SUCCESS.code;// 枚举类: 代码开发更加规范
        r.success = REnum.SUCCESS.flag;
        r.message = REnum.SUCCESS.message;
        return r;
    }
    public static R error (){
        R r = new R();
        r.code = REnum.ERROR.code;
        r.success = REnum.ERROR.flag;
        r.message = REnum.ERROR.message;
        return r;
    }
    public R data(String key, Object value){
        this.data.put(key,value);// 局部更新data
        return this;
    }
    public R data(Map<String,Object> map){
        this.data = map;// 整体替换data
        return this;
    }
    public R message(String message){
        this.message = message;
        return this;
    }
    public R code(Integer code){
        this.code = code;
        return this;
    }
}
